//
//  StyleNextViewController.m
//  OneDay
//
//  Created by lanou3g on 15/10/31.
//  Copyright © 2015年 oneDay. All rights reserved.
//

#import "StyleNextViewController.h"
#import "LoginViewController.h"

@interface StyleNextViewController () <UIWebViewDelegate, MONActivityIndicatorViewDelegate>
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) MONActivityIndicatorView *indicatorView;

@end

@implementation StyleNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    

    self.view.backgroundColor = [UIColor whiteColor];

    [self webView];
    [self addShareAndCollection];
    [self addIndicatorView];
    
    // 添加返回按钮
    UIBarButtonItem *leftB = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-reply-3.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backBar:)];
    self.navigationItem.leftBarButtonItem = leftB;
}

// 添加返回按钮
- (void)backBar:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIWebView *)webView
{
    if (!_webView) {
        NSString *urlStr;
        if (self.model == nil) {
            urlStr = [NSString stringWithFormat:@"%@", self.webUrl];
        }else{
            urlStr = [NSString stringWithFormat:@"%@", self.model.url];
        }
        
        self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
        
        self.webView.delegate = self;
        [self.view addSubview:_webView];
        
        NSURL *url = [NSURL URLWithString:urlStr];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
        
        [self.webView loadRequest:request];
        
    }
    return _webView;
}
#pragma mark - 添加加载等待动画
- (void)addIndicatorView
{
    _indicatorView = [[MONActivityIndicatorView alloc] init];
    _indicatorView.center = self.view.center;
    _indicatorView.delegate = self;
    
    _indicatorView.numberOfCircles = 5; // 圆点个数
    _indicatorView.internalSpacing = 5; // 圆点之间的距离
    _indicatorView.radius = 8; // 圆点半径
    //    _indicatorView.delay = 0;
    
    [self.view addSubview:_indicatorView];
    [_indicatorView startAnimating];
}

#pragma mark - 加载等待动画的代理方法
- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView circleBackgroundColorAtIndex:(NSUInteger)index
{
    switch (index) {
        case 0:
            return [UIColor orangeColor];
            break;
        case 1:
            return [UIColor grayColor];
            break;
        case 2:
            return [UIColor orangeColor];
            break;
        case 3:
            return [UIColor grayColor];
            break;
        default:
            return [UIColor orangeColor];
            break;;
    }
}

#pragma mark -- 添加分享，收藏按钮
- (void)addShareAndCollection
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth - 70, 40)];
    NSArray *array = @[@"heart_shape_small_on", @"ic_nav_share_black"];
    for (int i = 0; i < 2; i++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.frame = CGRectMake(kWidth - 130 + i * 35, 7.5, 25, 25);
        [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [button setImage:[UIImage imageNamed:array[i]] forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        button.tag = i + 1000;
        [view addSubview:button];
    }
    self.navigationItem.titleView  = view;
    
}
- (void)buttonAction:(UIButton *)sender
{
    if (sender.tag == 1000) {
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:nil message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        DataManager *manager = [DataManager shareManager];
        // 判断是否登录
        if ([manager isLogin] == NO) {
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            
            // 实现block
            loginVC.block = ^void(){
                NSLog(@"我已经登录成功");
            };
            
            UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:loginVC];
            [self presentViewController:navC animated:YES completion:nil];
        }else{
            LeanCloudObject *leanCloud = [[LeanCloudObject alloc] init];
            NSString *userName = [manager userName];
            // 先判断是否收藏过
            NSArray *webArray = [leanCloud selectStundetWithStyle:@"web" userName:userName];
            BOOL isHave = NO;
            for (WebObject *web in webArray) {
                if ([web.title isEqualToString:self.model.title]) {
                    isHave = YES;
                    break;
                }
            }
            if (isHave == NO) {
                [leanCloud creatObjectWithTitle:self.model.title imageUrl:self.model.photo needString:self.model.url style:@"web" userName:userName];
                [alertView show];
            }else{
                alertView.message = @"您已经收藏过了";
                [alertView show];
            }
            // 实现 2 秒回收
            [self performSelector:@selector(actionAlert:) withObject:alertView afterDelay:2];
        }
        
    }else{
        NSLog(@"分享");
        // 需要分享的文字
        NSString *str = [NSString stringWithFormat:@"OneDay推荐：想玩就去这里---%@", self.model.url];
        // 图片
        UIImageView *imageView = [[UIImageView alloc] init];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.model.photo] placeholderImage:nil];
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"56396d0ee0f55aff4d0010a0"
                                          shareText:str
                                         shareImage:imageView.image
                                    shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent, UMShareToDouban,UMShareToEmail,UMShareToFacebook, nil]
                                           delegate:nil];
    }
}
#pragma mark -- 实现2秒消失
- (void)actionAlert:(UIAlertView *)alertView
{
    [alertView dismissWithClickedButtonIndex:0 animated:YES];// 三秒回收
}


#pragma mark - webView 的代理方法
// 开始加载时触发
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"开始加载");
    [_indicatorView startAnimating];
    
}
// 加载完成触发
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"完成加载");
    [_indicatorView stopAnimating];
    self.tabBarController.tabBar.hidden = YES;
}
// 加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"加载失败");
}


//- (void)viewWillAppear:(BOOL)animated
//{
//    self.tabBarController.tabBar.hidden = YES;
//}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
    
}

@end
